package com.yihuan.controller;

import com.yihuan.entity.RecStyleEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.RecStyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/20 20:42
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class RecStyleController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    @Autowired
    RecStyleService recStyleService;
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findRecStyleByRecStyleList")
    public Map<String, Object> findRecStyleByRecStyleList(@RequestBody RecStyleEntity model, HttpSession session) {
        List<RecStyleEntity> findUserByUserList = recStyleService.findRecStyleByRecStyleList(model);
        if (findUserByUserList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_LIMIT_FALL);
            return map;
        }
        pageNumber = countUserList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("recStyle_List", findUserByUserList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countUserList(RecStyleEntity model) {
        count = recStyleService.countRecStyleList();
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 模糊分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findRecStyleByRecStyleConcatList")
    public Map<String, Object> findRecStyleByRecStyleConcatList(@RequestBody RecStyleEntity model, HttpSession session) {
        List<RecStyleEntity> findUserByUserConcatList = recStyleService.findRecStyleByRecStyleConcatList(model);
        if (findUserByUserConcatList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_LIMIT_Concat_FALL);
            return map;
        }
        pageNumber = countRecStyleConcatList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("recStyle_List", findUserByUserConcatList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_LIMIT_Concat_SUCCESS);
        return map;
    }
    
    /**
     * 统计模糊分页查询数据
     *
     * @param model
     * @return
     */
    public int countRecStyleConcatList(RecStyleEntity model) {
        count = recStyleService.countRecStyleConcatList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 更改回收品类型信息状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateRecStyleByRecStyleState")
    public Map<String, Object> updateRecStyleByRecStyleState(@RequestBody RecStyleEntity model, HttpSession session) {
        int updateUserByUserState = recStyleService.updateRecStyleByRecStyleState(model);
        //判断数据是否存在
        if (updateUserByUserState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_STATE_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.RECSTYLE_STATE_SUCCESS);
        return map;
    }
    
    /**
     * 查询回收品类型信息名称 按商品类型编号
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findRecStyleByRecStyID")
    public Map<String, Object> findRecStyleByRecStyID(@RequestBody RecStyleEntity model, HttpSession session) {
        List<RecStyleEntity> findRecStyleByRecStyID = recStyleService.findRecStyleByRecStyID(model);
        //判断数据是否存在
        if (findRecStyleByRecStyID.size() == 1) {
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("recStyle_List", findRecStyleByRecStyID);
            map.put("explain ", MessageConstant.RECSTYLE_UPATE_ID_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain ", MessageConstant.RECSTYLE_UPATE_ID_FALL);
        return map;
    }
    
    /**
     * 更改回收品类型信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateRecStyleByRecStyleName")
    public Map<String, Object> updateRecStyleByRecStyleName(@RequestBody RecStyleEntity model, HttpSession session) {
        int updateUserByUserState = recStyleService.updateRecStyleByRecStyleName(model);
        //判断数据是否存在
        if (updateUserByUserState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_UPATE_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.RECSTYLE_UPATE_SUCCESS);
        return map;
    }
    
    /**
     * 登记回收品类型
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/insertRecStyleByRecStyleData")
    public Map<String, Object> insertRecStyleByRecStyleData(@RequestBody RecStyleEntity model, HttpSession session) {
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        model.setRecStyDate(sf.format(d));
        //状态
        model.setRecStyState(1);
        int insertRecStyleByRecStyleData = recStyleService.insertRecStyleByRecStyleData(model);
        //判断数据是否存在
        if (insertRecStyleByRecStyleData == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_INSERT_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.RECSTYLE_INSERT_SUCCESS);
        return map;
    }
    
    /**
     * 查询回收品类型信息名称
     *
     * @param session
     * @return
     */
    @RequestMapping("/findRecStyleByRecStyleData")
    public Map<String, Object> findRecStyleByRecStyleData(HttpSession session) {
        List<RecStyleEntity> findRecStyleByRecStyID = recStyleService.findRecStyleByRecStyleData();
        //判断数据是否存在
        if (findRecStyleByRecStyID.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_SELECT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("recStyle_List", findRecStyleByRecStyID);
        map.put("explain ", MessageConstant.RECSTYLE_SELECT_SUCCESS);
        return map;
    }
}
